<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>教师管理</span>
      </div>
    </template>
    <el-table :data="teachers" style="width: 100%">
      <el-table-column label="序号" width="50" type="index"></el-table-column>
      <el-table-column label="教师姓名" prop="teacherName"></el-table-column>
      <el-table-column label="关联班级" prop="clazzId"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="title" width="30%">
      <el-form :model="teacherModel" :rules="rules" label-width="100px">
        <el-form-item label="教师姓名" prop="teacherName">
          <el-input v-model="teacherModel.teacherName" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="关联班级" prop="clazzId">
          <el-input v-model="teacherModel.clazzId"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="updateTeacher()">确认</el-button>
        </span>
      </template>
    </el-dialog>
  </el-card>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { Edit } from '@element-plus/icons-vue';
import { fetchTeacherList, updateTeacherClazz } from '@/api/teacherService.js';

const teachers = ref([]);
const dialogVisible = ref(false);
const teacherModel = ref({ id: null, teacherName: '', clazzId: '' });
const title = ref('');
const rules = {
  clazzId: [{ required: true, message: '请输入关联班级', trigger: 'blur' }]
};

const fetchTeachers = async () => {
  try {
    const result = await fetchTeacherList({
      teacherName: '',
      page: 1,
      pageSize: 10
    });

    if (result.code === 0 && result.data && Array.isArray(result.data.records)) {
      teachers.value = result.data.records;
    } else {
      ElMessage.error('未能正确获取教师 列表数据');
    }
  } catch (error) {
    console.error('Fetch teachers error:', error);
    ElMessage.error('获取教师列表失败');
  }
};

const updateTeacher = async () => {
  try {
    const result = await updateTeacherClazz({
      id: teacherModel.value.id,
      clazzId: teacherModel.value.clazzId
    });
    fetchTeachers();
    ElMessage.success(result.msg || '修改成功');
    dialogVisible.value = false;

  } catch (error) {
    ElMessage.error('修改教师关联班级失败');
  }
};

const showDialog = (row = null) => {
  dialogVisible.value = true;
  title.value = '编辑教师关联班级';
  if (row) {
    teacherModel.value = { ...row };
  } else {
    teacherModel.value = { id: null, teacherName: '', clazzId: '' };
  }
};

onMounted(() => {
  fetchTeachers();
});
</script>

<style scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}
</style>
